package com.amazon.inapp.purchasing;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.json.JSONArray;
import org.json.JSONObject;

final class SandboxResponseHandler
  implements ResponseHandler
{
  private static final String TAG = "SandboxResponseHandler";
  private final HandlerAdapter _handler = HandlerManager.getMainHandlerAdapter();

  private Item getItem(String paramString, JSONObject paramJSONObject)
  {
    Item.ItemType localItemType = Item.ItemType.valueOf(paramJSONObject.optString("itemType"));
    return new Item(paramString, paramJSONObject.optString("price"), localItemType, paramJSONObject.optString("title"), paramJSONObject.optString("description"), paramJSONObject.optString("smallIconUrl"));
  }

  private ItemDataResponse getItemDataResponse(Intent paramIntent)
  {
    ItemDataResponse.ItemDataRequestStatus localItemDataRequestStatus1 = ItemDataResponse.ItemDataRequestStatus.FAILED;
    try
    {
      localJSONObject1 = new JSONObject(paramIntent.getStringExtra("itemDataOutput"));
      str2 = localJSONObject1.optString("requestId");
    }
    catch (Exception localException2)
    {
      try
      {
        localItemDataRequestStatus1 = ItemDataResponse.ItemDataRequestStatus.valueOf(localJSONObject1.optString("status"));
        if (localItemDataRequestStatus1 != ItemDataResponse.ItemDataRequestStatus.FAILED)
          localObject3 = new HashSet();
      }
      catch (Exception localException2)
      {
        while (true)
        {
          try
          {
            JSONObject localJSONObject1;
            HashMap localHashMap2 = new HashMap();
            try
            {
              JSONArray localJSONArray = localJSONObject1.optJSONArray("unavailableSkus");
              if (localJSONArray != null)
              {
                int i = 0;
                if (i < localJSONArray.length())
                {
                  ((Set)localObject3).add(localJSONArray.getString(i));
                  i++;
                  continue;
                }
              }
              JSONObject localJSONObject2 = localJSONObject1.optJSONObject("items");
              if (localJSONObject2 == null)
                continue;
              Iterator localIterator = localJSONObject2.keys();
              if (!localIterator.hasNext())
                continue;
              String str3 = (String)localIterator.next();
              localHashMap2.put(str3, getItem(str3, localJSONObject2.optJSONObject(str3)));
              continue;
            }
            catch (Exception localException3)
            {
              localHashMap1 = localHashMap2;
              localObject2 = localItemDataRequestStatus1;
              str1 = str2;
              localObject1 = localException3;
            }
            Log.e("SandboxResponseHandler", "Error parsing item data output", (Throwable)localObject1);
            return new ItemDataResponse(str1, (Set)localObject3, (ItemDataResponse.ItemDataRequestStatus)localObject2, localHashMap1);
            localHashMap1 = localHashMap2;
            localObject4 = localObject3;
            Object localObject3 = localObject4;
            localObject2 = localItemDataRequestStatus1;
            str1 = str2;
            continue;
            localException1 = localException1;
            localObject1 = localException1;
            localObject2 = localItemDataRequestStatus1;
            localHashMap1 = null;
            localObject3 = null;
            str1 = null;
            continue;
            localException2 = localException2;
            ItemDataResponse.ItemDataRequestStatus localItemDataRequestStatus2 = localItemDataRequestStatus1;
            str1 = str2;
            localObject1 = localException2;
            localObject2 = localItemDataRequestStatus2;
            localHashMap1 = null;
            localObject3 = null;
            continue;
          }
          catch (Exception localException4)
          {
            String str2;
            Object localObject2 = localItemDataRequestStatus1;
            String str1 = str2;
            Object localObject1 = localException4;
            localHashMap1 = null;
            continue;
          }
          HashMap localHashMap1 = null;
          Object localObject4 = null;
        }
      }
    }
  }

  // ERROR //
  private PurchaseResponse getPurchaseResponse(Intent paramIntent)
  {
    // Byte code:
    //   0: getstatic 158	com/amazon/inapp/purchasing/PurchaseResponse$PurchaseRequestStatus:FAILED	Lcom/amazon/inapp/purchasing/PurchaseResponse$PurchaseRequestStatus;
    //   3: astore_2
    //   4: new 30	org/json/JSONObject
    //   7: dup
    //   8: aload_1
    //   9: ldc 160
    //   11: invokevirtual 70	android/content/Intent:getStringExtra	(Ljava/lang/String;)Ljava/lang/String;
    //   14: invokespecial 73	org/json/JSONObject:<init>	(Ljava/lang/String;)V
    //   17: astore_3
    //   18: aload_3
    //   19: ldc 75
    //   21: invokevirtual 34	org/json/JSONObject:optString	(Ljava/lang/String;)Ljava/lang/String;
    //   24: astore 11
    //   26: aload 11
    //   28: astore 6
    //   30: aload_3
    //   31: ldc 162
    //   33: invokevirtual 34	org/json/JSONObject:optString	(Ljava/lang/String;)Ljava/lang/String;
    //   36: astore 13
    //   38: aload 13
    //   40: astore 5
    //   42: aload_3
    //   43: ldc 164
    //   45: invokevirtual 34	org/json/JSONObject:optString	(Ljava/lang/String;)Ljava/lang/String;
    //   48: invokestatic 167	com/amazon/inapp/purchasing/PurchaseResponse$PurchaseRequestStatus:valueOf	(Ljava/lang/String;)Lcom/amazon/inapp/purchasing/PurchaseResponse$PurchaseRequestStatus;
    //   51: astore 15
    //   53: aload 15
    //   55: astore 7
    //   57: aload_3
    //   58: ldc 169
    //   60: invokevirtual 114	org/json/JSONObject:optJSONObject	(Ljava/lang/String;)Lorg/json/JSONObject;
    //   63: astore 16
    //   65: aconst_null
    //   66: astore 10
    //   68: aload 16
    //   70: ifnull +15 -> 85
    //   73: aload_0
    //   74: aload 16
    //   76: invokespecial 173	com/amazon/inapp/purchasing/SandboxResponseHandler:getReceipt	(Lorg/json/JSONObject;)Lcom/amazon/inapp/purchasing/Receipt;
    //   79: astore 17
    //   81: aload 17
    //   83: astore 10
    //   85: new 175	com/amazon/inapp/purchasing/PurchaseResponse
    //   88: dup
    //   89: aload 6
    //   91: aload 5
    //   93: aload 10
    //   95: aload 7
    //   97: invokespecial 178	com/amazon/inapp/purchasing/PurchaseResponse:<init>	(Ljava/lang/String;Ljava/lang/String;Lcom/amazon/inapp/purchasing/Receipt;Lcom/amazon/inapp/purchasing/PurchaseResponse$PurchaseRequestStatus;)V
    //   100: areturn
    //   101: astore 4
    //   103: aconst_null
    //   104: astore 5
    //   106: aconst_null
    //   107: astore 6
    //   109: aload_2
    //   110: astore 7
    //   112: aload 4
    //   114: astore 8
    //   116: ldc 10
    //   118: ldc 180
    //   120: aload 8
    //   122: invokestatic 146	android/util/Log:e	(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Throwable;)I
    //   125: pop
    //   126: aconst_null
    //   127: astore 10
    //   129: goto -44 -> 85
    //   132: astore 12
    //   134: aload 12
    //   136: astore 8
    //   138: aload_2
    //   139: astore 7
    //   141: aconst_null
    //   142: astore 5
    //   144: goto -28 -> 116
    //   147: astore 14
    //   149: aload_2
    //   150: astore 7
    //   152: aload 14
    //   154: astore 8
    //   156: goto -40 -> 116
    //   159: astore 8
    //   161: goto -45 -> 116
    //
    // Exception table:
    //   from	to	target	type
    //   4	26	101	java/lang/Exception
    //   30	38	132	java/lang/Exception
    //   42	53	147	java/lang/Exception
    //   57	65	159	java/lang/Exception
    //   73	81	159	java/lang/Exception
  }

  private PurchaseUpdatesResponse getPurchaseUpdatesResponse(Intent paramIntent)
  {
    Object localObject1 = null;
    PurchaseUpdatesResponse.PurchaseUpdatesRequestStatus localPurchaseUpdatesRequestStatus = PurchaseUpdatesResponse.PurchaseUpdatesRequestStatus.FAILED;
    try
    {
      localJSONObject = new JSONObject(paramIntent.getStringExtra("purchaseUpdatesOutput"));
      str3 = localJSONObject.optString("requestId");
    }
    catch (Exception localException7)
    {
      try
      {
        localPurchaseUpdatesRequestStatus = PurchaseUpdatesResponse.PurchaseUpdatesRequestStatus.valueOf(localJSONObject.optString("status"));
        str4 = localJSONObject.optString("offset");
      }
      catch (Exception localException7)
      {
        try
        {
          boolean bool2 = localJSONObject.optBoolean("isMore");
          bool1 = bool2;
        }
        catch (Exception localException7)
        {
          try
          {
            str5 = localJSONObject.optString("userId");
          }
          catch (Exception localException7)
          {
            try
            {
              if (localPurchaseUpdatesRequestStatus == PurchaseUpdatesResponse.PurchaseUpdatesRequestStatus.SUCCESSFUL)
                localHashSet3 = new HashSet();
            }
            catch (Exception localException7)
            {
              while (true)
              {
                try
                {
                  localHashSet4 = new HashSet();
                }
                catch (Exception localException7)
                {
                  try
                  {
                    JSONObject localJSONObject;
                    JSONArray localJSONArray1 = localJSONObject.optJSONArray("receipts");
                    if (localJSONArray1 != null)
                    {
                      int j = 0;
                      if (j < localJSONArray1.length())
                      {
                        localHashSet3.add(getReceipt(localJSONArray1.optJSONObject(j)));
                        j++;
                        continue;
                      }
                    }
                    JSONArray localJSONArray2 = localJSONObject.optJSONArray("revokedSkus");
                    int i = 0;
                    if (localJSONArray2 != null)
                      if (i < localJSONArray2.length())
                      {
                        localHashSet4.add(localJSONArray2.getString(i));
                        i++;
                        continue;
                      }
                    localHashSet5 = localHashSet3;
                    str1 = str5;
                    localHashSet1 = localHashSet4;
                    localHashSet2 = localHashSet5;
                    localObject1 = str3;
                    Object localObject3 = str4;
                    return new PurchaseUpdatesResponse(localObject1, str1, localPurchaseUpdatesRequestStatus, localHashSet2, localHashSet1, Offset.fromString((String)localObject3), bool1);
                    localException1 = localException1;
                    localObject2 = localException1;
                    localHashSet1 = null;
                    localHashSet2 = null;
                    boolean bool1 = false;
                    str1 = null;
                    str2 = null;
                    Log.e("SandboxResponseHandler", "Error parsing purchase updates output", (Throwable)localObject2);
                    localObject3 = str2;
                    continue;
                    localException2 = localException2;
                    localObject1 = str3;
                    localObject2 = localException2;
                    str2 = null;
                    str1 = null;
                    localHashSet2 = null;
                    localHashSet1 = null;
                    bool1 = false;
                    continue;
                    localException3 = localException3;
                    str2 = str4;
                    localObject1 = str3;
                    localObject2 = localException3;
                    str1 = null;
                    localHashSet2 = null;
                    localHashSet1 = null;
                    bool1 = false;
                    continue;
                    localException4 = localException4;
                    localObject1 = str3;
                    localObject2 = localException4;
                    str2 = str4;
                    str1 = null;
                    localHashSet2 = null;
                    localHashSet1 = null;
                    continue;
                    localException5 = localException5;
                    str1 = str5;
                    localObject1 = str3;
                    localObject2 = localException5;
                    str2 = str4;
                    localHashSet2 = null;
                    localHashSet1 = null;
                    continue;
                    localException7 = localException7;
                    str2 = str4;
                    localHashSet2 = localHashSet3;
                    str1 = str5;
                    localObject1 = str3;
                    localObject2 = localException7;
                    localHashSet1 = null;
                    continue;
                  }
                  catch (Exception localException6)
                  {
                    String str3;
                    String str4;
                    String str5;
                    HashSet localHashSet3;
                    localObject1 = str3;
                    Object localObject2 = localException6;
                    String str1 = str5;
                    HashSet localHashSet1 = localHashSet4;
                    String str2 = str4;
                    HashSet localHashSet2 = localHashSet3;
                    continue;
                  }
                }
                HashSet localHashSet4 = null;
                HashSet localHashSet5 = null;
              }
            }
          }
        }
      }
    }
  }

  private Receipt getReceipt(JSONObject paramJSONObject)
    throws ParseException
  {
    String str1 = paramJSONObject.optString("sku");
    Item.ItemType localItemType = Item.ItemType.valueOf(paramJSONObject.optString("itemType"));
    JSONObject localJSONObject = paramJSONObject.optJSONObject("subscripionPeriod");
    SimpleDateFormat localSimpleDateFormat;
    Date localDate1;
    String str2;
    Date localDate2;
    if (localItemType == Item.ItemType.SUBSCRIPTION)
    {
      localSimpleDateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
      localDate1 = localSimpleDateFormat.parse(localJSONObject.optString("startTime"));
      str2 = localJSONObject.optString("endTime");
      localDate2 = null;
      if (str2 != null)
      {
        int i = str2.length();
        localDate2 = null;
        if (i != 0)
          break label122;
      }
    }
    for (SubscriptionPeriod localSubscriptionPeriod = new SubscriptionPeriod(localDate1, localDate2); ; localSubscriptionPeriod = null)
    {
      return new Receipt(str1, localItemType, false, localSubscriptionPeriod, paramJSONObject.optString("token"));
      label122: localDate2 = localSimpleDateFormat.parse(str2);
      break;
    }
  }

  // ERROR //
  private GetUserIdResponse getUserIdResponse(Intent paramIntent)
  {
    // Byte code:
    //   0: getstatic 268	com/amazon/inapp/purchasing/GetUserIdResponse$GetUserIdRequestStatus:FAILED	Lcom/amazon/inapp/purchasing/GetUserIdResponse$GetUserIdRequestStatus;
    //   3: astore_2
    //   4: new 30	org/json/JSONObject
    //   7: dup
    //   8: aload_1
    //   9: ldc_w 270
    //   12: invokevirtual 70	android/content/Intent:getStringExtra	(Ljava/lang/String;)Ljava/lang/String;
    //   15: invokespecial 73	org/json/JSONObject:<init>	(Ljava/lang/String;)V
    //   18: astore_3
    //   19: aload_3
    //   20: ldc 75
    //   22: invokevirtual 34	org/json/JSONObject:optString	(Ljava/lang/String;)Ljava/lang/String;
    //   25: astore 11
    //   27: aload 11
    //   29: astore 5
    //   31: aload_3
    //   32: ldc 77
    //   34: invokevirtual 34	org/json/JSONObject:optString	(Ljava/lang/String;)Ljava/lang/String;
    //   37: invokestatic 273	com/amazon/inapp/purchasing/GetUserIdResponse$GetUserIdRequestStatus:valueOf	(Ljava/lang/String;)Lcom/amazon/inapp/purchasing/GetUserIdResponse$GetUserIdRequestStatus;
    //   40: astore 13
    //   42: aload 13
    //   44: astore 7
    //   46: getstatic 275	com/amazon/inapp/purchasing/GetUserIdResponse$GetUserIdRequestStatus:SUCCESSFUL	Lcom/amazon/inapp/purchasing/GetUserIdResponse$GetUserIdRequestStatus;
    //   49: astore 14
    //   51: aconst_null
    //   52: astore 15
    //   54: aload 7
    //   56: aload 14
    //   58: if_acmpne +15 -> 73
    //   61: aload_3
    //   62: ldc 162
    //   64: invokevirtual 34	org/json/JSONObject:optString	(Ljava/lang/String;)Ljava/lang/String;
    //   67: astore 16
    //   69: aload 16
    //   71: astore 15
    //   73: aload 7
    //   75: astore 17
    //   77: aload 15
    //   79: astore 10
    //   81: aload 17
    //   83: astore 9
    //   85: new 277	com/amazon/inapp/purchasing/GetUserIdResponse
    //   88: dup
    //   89: aload 5
    //   91: aload 9
    //   93: aload 10
    //   95: invokespecial 280	com/amazon/inapp/purchasing/GetUserIdResponse:<init>	(Ljava/lang/String;Lcom/amazon/inapp/purchasing/GetUserIdResponse$GetUserIdRequestStatus;Ljava/lang/String;)V
    //   98: areturn
    //   99: astore 4
    //   101: aconst_null
    //   102: astore 5
    //   104: aload 4
    //   106: astore 6
    //   108: aload_2
    //   109: astore 7
    //   111: ldc 10
    //   113: ldc_w 282
    //   116: aload 6
    //   118: invokestatic 146	android/util/Log:e	(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Throwable;)I
    //   121: pop
    //   122: aload 7
    //   124: astore 9
    //   126: aconst_null
    //   127: astore 10
    //   129: goto -44 -> 85
    //   132: astore 12
    //   134: aload_2
    //   135: astore 7
    //   137: aload 12
    //   139: astore 6
    //   141: goto -30 -> 111
    //   144: astore 6
    //   146: goto -35 -> 111
    //
    // Exception table:
    //   from	to	target	type
    //   4	27	99	java/lang/Exception
    //   31	42	132	java/lang/Exception
    //   46	51	144	java/lang/Exception
    //   61	69	144	java/lang/Exception
  }

  private void handleItemDataResponse(Intent paramIntent)
  {
    Runnable local2 = new Runnable()
    {
      public void run()
      {
        if (Logger.isTraceOn())
          Logger.trace("SandboxResponseHandler", "Running Runnable for itemDataResponse with requestId: " + this.val$response.getRequestId());
        PurchasingObserver localPurchasingObserver = PurchasingManager.getPurchasingObserver();
        if (localPurchasingObserver != null)
          localPurchasingObserver.onItemDataResponse(this.val$response);
      }
    };
    this._handler.post(local2);
  }

  private void handlePurchaseResponse(Intent paramIntent)
  {
    Runnable local4 = new Runnable()
    {
      public void run()
      {
        if (Logger.isTraceOn())
          Logger.trace("SandboxResponseHandler", "Running Runnable for purchaseResponse with requestId: " + this.val$response.getRequestId());
        PurchasingObserver localPurchasingObserver = PurchasingManager.getPurchasingObserver();
        if (localPurchasingObserver != null)
          localPurchasingObserver.onPurchaseResponse(this.val$response);
      }
    };
    this._handler.post(local4);
  }

  private void handlePurchaseUpdatesResponse(Intent paramIntent)
  {
    Runnable local1 = new Runnable()
    {
      public void run()
      {
        if (Logger.isTraceOn())
          Logger.trace("SandboxResponseHandler", "Running Runnable for purchaseUpdatesResponse with requestId: " + this.val$response.getRequestId());
        PurchasingObserver localPurchasingObserver = PurchasingManager.getPurchasingObserver();
        if (localPurchasingObserver != null)
          localPurchasingObserver.onPurchaseUpdatesResponse(this.val$response);
      }
    };
    this._handler.post(local1);
  }

  private void handleUserIdResponse(Intent paramIntent)
  {
    Runnable local3 = new Runnable()
    {
      public void run()
      {
        if (Logger.isTraceOn())
          Logger.trace("SandboxResponseHandler", "Running Runnable for userIdResponse with requestId: " + this.val$response.getRequestId());
        PurchasingObserver localPurchasingObserver = PurchasingManager.getPurchasingObserver();
        if (localPurchasingObserver != null)
          localPurchasingObserver.onGetUserIdResponse(this.val$response);
      }
    };
    this._handler.post(local3);
  }

  public void handleResponse(Context paramContext, Intent paramIntent)
  {
    if (Logger.isTraceOn())
      Logger.trace("SandboxResponseHandler", "handleResponse");
    String str;
    try
    {
      str = paramIntent.getExtras().getString("responseType");
      if (str.equalsIgnoreCase("com.amazon.testclient.iap.purchase"))
      {
        handlePurchaseResponse(paramIntent);
        return;
      }
      if (str.equalsIgnoreCase("com.amazon.testclient.iap.appUserId"))
      {
        handleUserIdResponse(paramIntent);
        return;
      }
    }
    catch (Exception localException)
    {
      Log.e("SandboxResponseHandler", "Error handling response.", localException);
      return;
    }
    if (str.equalsIgnoreCase("com.amazon.testclient.iap.itemData"))
    {
      handleItemDataResponse(paramIntent);
      return;
    }
    if (str.equalsIgnoreCase("com.amazon.testclient.iap.purchaseUpdates"))
      handlePurchaseUpdatesResponse(paramIntent);
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.amazon.inapp.purchasing.SandboxResponseHandler
 * JD-Core Version:    0.6.2
 */